﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class admin_module_usergroup : System.Web.UI.UserControl
{
    user_groupBus usergroupbus = new user_groupBus();
    user_group usergroup;
    private int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
        }
    }
    public void loadData()
    {
        id = 0;
        string[] permission = null;
        this.getListpermission();
        if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null)
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            usergroup = usergroupbus.getUserGroupByID(id);
            txtname.Text = usergroup.Name;
            permission = this.getArrayPermission(usergroup.Permission);
            
            foreach (ListItem i in listpermission.Items)
            {
                foreach (string str in permission)
                {
                    if (i.Value.Equals(str))
                    {
                        i.Selected = true;
                    }
                }
            }

        }
        else
        {
            usergroup = new user_group(0, "", "");
        }
    }
    public void getListpermission()
    {
        DirectoryInfo dinfo = new DirectoryInfo(Server.MapPath("~//admin//module"));
        FileInfo[] file = dinfo.GetFiles("*.ascx");
        foreach (FileInfo f in file)
        {
            string output = this.spilit(f.Name);
            listpermission.Items.Add(new ListItem(output, output));
        }
        
    }
    public string spilit(string name)
    {
        string output = name.Substring(0,name.IndexOf("."));
        return output;
    }
    public string[] getArrayPermission(string txt)
    {
        string[] array = txt.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        return array;
    }
    public string setPermission()
    {
        string output = "";
        foreach (ListItem i in listpermission.Items)
        {
            if (i.Selected)
            {
                if (listpermission.Items.IndexOf(i) != 0)
                {
                    output += "," + i.Value;
                }
                else
                {
                    output = i.Value;
                }
            }
        }
        return output;
    }
    protected void OK_UserGroup_Click(object sender, ImageClickEventArgs e)
    {
        if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null)
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
        }
        usergroupbus.EditUserGroup(id, txtname.Text, setPermission());
        Response.Redirect("default.aspx?action=list_usergroup");

    }
    protected void Cancel_UserGroup_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("default.aspx?action=list_usergroup");
    }
}